﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms.Signups
{
    public partial class frmSignupsAddStNum : Form
    {
        // Constructor
        public frmSignupsAddStNum()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Form Load
        private void frmSignupsAddStNum_Load(object sender, EventArgs e)
        {
            this.lblTitle.Text = Globals.ActiveCompetition.Name;
            
            //this.performancesTableAdapter.FillByCompetitionId(this.aDbDataSet.Performances, Globals.ActiveCompetition.ID);
            DatabaseHelper.FillPerformancesByCompetitionId(this.aDbDataSet.Performances, Globals.ActiveCompetition.ID);
            performancesDataGridView.ReadOnly = true;
            btnAdd.Focus();
            
        }

        // btnAdd Click
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DataRowView drv = (DataRowView)performancesDataGridView.SelectedRows[0].DataBoundItem;
                string compId = drv["CompetitorId"].ToString();

                if (compId == "0")
                {
                    drv["StartNumber"] = Convert.ToInt32(nudStNum.Value);
                    performancesTableAdapter.Update(drv.Row);

                    nudStNum.Value += 1;

                    if (performancesDataGridView.SelectedRows[0].Index + 1 < performancesDataGridView.RowCount)
                        performancesDataGridView.Rows[performancesDataGridView.SelectedRows[0].Index + 1].Selected = true;
                }
                else
                {
                    int lastUpdatedIndex = 0;
                    for (int i = 0; i < performancesDataGridView.Rows.Count; i++ )
                    {
                        DataGridViewRow dgvr = performancesDataGridView.Rows[i];

                        if (dgvr.DataBoundItem != null)
                        {
                            DataRow dr = ((DataRowView)dgvr.DataBoundItem).Row;
                            
                            if (dr["CompetitorId"].ToString() == compId)
                            {
                                dr["StartNumber"] = Convert.ToInt32(nudStNum.Value);
                                performancesTableAdapter.Update(dr);

                                lastUpdatedIndex = i;
                            }
                        }
                    }

                    nudStNum.Value += 1;
                    
                    if (lastUpdatedIndex + 1 < performancesDataGridView.RowCount)
                        performancesDataGridView.Rows[lastUpdatedIndex + 1].Selected = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Priradenie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }








    }
}
